/**
 * <p>新开一个窗口</p>
 * @function
 * @param url 页面访问地址
 * @param max 是否最大化,true为最大化
 * @return {window} 打开的窗口对象
 */
zxt.dom.openWindow = function(url, max){
	var nowTime = new Date();
  var winLabel = nowTime.getTime();
    
  var parameter = "toolbar=no,location=no,directories=no,status=yes,menubar=no,resizable=yes,scrollbars=yes,";
  if (max == true) {
    parameter += "width=" + (screen.availWidth - 10) + ",height=" + (screen.availHeight - 30) + ",top=0," + "left=0";
  } else {
    parameter += "width=900,height=600,top=" + ((screen.availHeight - 600) / 2)+ ",left=" + ((screen.availWidth - 900) / 2);
  }
  return window.open(url, winLabel, parameter); 
};

/**
 * <p>新开一个页面</p>
 * @function
 * @param path  路径
 * @param page 页面名
 * @param md   方法名
 * @param dw   zxt.data.DataWrapper
 * @param max 是否最大化,true为最大化
 * @return {window} 打开的窗口对象
 */
zxt.dom.openPage = function(path, page, md, dw, max){
	var nowTime = new Date();
  var winLabel = nowTime.getTime();
    
  var parameter = "toolbar=no,location=no,directories=no,status=yes,menubar=no,resizable=yes,scrollbars=yes,";
  if (max == true) {
    parameter += "width=" + (screen.availWidth - 10) + ",height=" + (screen.availHeight - 30) + ",top=0," + "left=0";
  } else {
    parameter += "width=900,height=600,top=" + ((screen.availHeight - 600) / 2)+ ",left=" + ((screen.availWidth - 900) / 2);
  }
  
  win = window.open("about:blank", winLabel, parameter);
  EiCommunicator.submit(path, page, dw, md, winLabel);    
  //EiCommunicator.submitURL("http://www.baidu.com", dw, null, winLabel);
  return win;
};	


zxt.dom.setCursorAtEnd = function(oTextbox) {
	/**
	if (oTextbox.createTextRange) {
		var r = oTextbox.createTextRange();
		r.moveStart('character', (oTextbox.value.length));
		r.collapse();
		r.select();
	}
	**/
};

/**
 * <p>
 * 将目标DOM节点国际化
 * </p>
 * 
 * @private
 * @function
 * @param {DOM} element DOM节点
 */
zxt.dom.i18n = function(dom) {	
	var j = $(dom);	
	var _key = j.attr("zxt.key");	
	var _trvalue = "";
	var _local = j.attr("zxt.local");
	
	if(zxt.util.isAvailable(_key)){
		_trvalue = $zxt.localizer().localize(_key, _local);	
		if ( !zxt.util.isValue(_trvalue) ){
			_trvalue = "[" + _key + "]";	
		};
		
		var oText = document.createTextNode(_trvalue);   
		if( zxt.cfg.replaceDivI18N ){//false
			dom.parentNode.insertBefore(oText, dom);	
			dom.parentNode.removeChild(dom);	
		} else {
			dom.appendChild(oText);
		};
		return;
	};
		
	for(var i=0;i<dom.attributes.length;i++){
		_key = dom.attributes[i].name;
		_trvalue = $zxt.localizer().translate(dom.getAttribute(_key));
		dom.setAttribute(_key, _trvalue);		
		//var _value = dom.attributes[i].value;
		//dom.attributes[i].value = _trvalue;
	};
};

/**
 * <p>
 * 根据DOM节点创建ZXT控件
 * </p>
 * 
 * @private
 * @function
 * @param {DOM} element DOM节点
 * @param {boolean} rebuild 重新构建ZXT控件标志 true:重新构建ZXT控件
 */
zxt.dom.zxt = function(dom, rebuild) {
	var rbd = rebuild || false;
	var j = $(dom);
	
	for(var i=0;i<dom.attributes.length;i++){
		var _key = dom.attributes[i].name;		
		var _value = dom.getAttribute(_key);		
		var _trvalue = $zxt.localizer().translate(_value);		
		dom.setAttribute(_key, _trvalue);
	};
	
	var zxt_type = j.attr("zxt");	
	var component = eval(zxt_type+".parse(dom);");
	dom["component"] = component;
	
	var s_conf = j.attr("zxt.conf");	
	if ( zxt.util.isLengthString(s_conf) ){
		var _conf = eval( "({"+ s_conf +"})" );	
		component.config(_conf);
	};	
		
	var _configFuncName = j.attr("zxt.configFunc");	
	var _configFunc = window[_configFuncName];
	if ( zxt.util.isFunction(_configFunc) ){
		_configFunc.call(window, component);	
	};
	
	var _id = j.attr("id");
	if ( zxt.util.isLengthString(_id) ){
		component.id(_id);
	};
	
	if (rbd){
		var oldw = $zxt.getComponent(_id);
		if ( oldw != null ){
			$zxt.unregister(oldw);
			if ( component.instanceOf(zxt.ui.Interactor) ){
				component.setValue(oldw.getValue());
			}
		}	
	}
	
	$zxt.register(component);
	
	
	if ( component.instanceOf(zxt.ui.Widget) ){
		var widget = component;
			
		var _mode = j.attr("zxt.mode");	
		if ( zxt.util.isValue(_mode) ){
			widget.mode(_mode);
		};
		var _widgetDom = widget.render();	
		// 容器被替换的情况
		if ( zxt.cfg.replaceContainer ){			
			jDom.parentNode.replaceChild(_widgetDom, dom);		
		// 容器不被替换的情况
		}	else {		
			j.empty();
			j.append(_widgetDom);
		};				
	};

	return component;	
};	

/**
 * 延迟加载ZXT控件
 * @function
 * @param {DOM} element DOM节点
 */
zxt.dom.lazyDom = function(dom) {
	var j = $(dom);
	var zxt_type = j.attr("zxt");
	var _id = j.attr("id");
	var _value = j.attr("zxt.value");
	var _default = j.attr("zxt.default");
	var proxy = new zxt.ui.WidgetProxy(_id);
	dom["component"]=proxy;
	
	if (_value != undefined)
		proxy.setValue(_value);
	else if (_default != undefined)
		proxy.setValue(_default);
	
	$zxt.register(proxy);
	return proxy;
};



            

